\usepackage{listings}
\lstset{language=Java}

\usepackage{graphicx}
\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  urlcolor=blue,
  linkcolor=black
}
\mode<article>{\usepackage{fullpage}}

\title{Lecture Two -- A Bit More Java}
\author{Matt Bone}
\date{\today}

\begin{document}

\mode<article>{\maketitle}
\tableofcontents
\mode<article>{\pagebreak}
\mode<presentation>{\frame{\titlepage}}
\mode<article>{\setlength{\parskip}{.25cm}}


\section{Exception Overview}

\subsection{What is an Exception}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{What is an Exception?}}

  Exceptions are a way of handling errors.  They are conceptually
  similar to hardware interrupt.

\end{frame}
Exceptions are a way of dealing with errors and are found in many
languages including Java, C++, Python, Ruby, Common Lisp and many
more.  An exception itself is an error condition raised by a chunk of
code.  Much like a hardware interrupt, the exception alters the normal
flow of code in the program.  In the worst case, an exception will
stop the execution of a program.  However, we can write exception
handlers that take care of conditions and allow the program continue
normally.

\subsection{Propogation}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{Propogation}}
  Exceptions propagate: ``If you call something that fails and you
  can't recover you have to fail." --
  \href{http://video.google.com/videoplay?docid=448441135356213813}{Seibel} 
\end{frame}


\section{Exceptions in Java}

\subsection{Checked and Unchecked Exceptions}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{Checked and Unchecked Exceptions}}
  \begin{itemize}
  \item checked (subclass of
  \href{http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html}{Exception}
  class)
  \item unchecked (subclass of
 \href{http://java.sun.com/j2se/1.5.0/docs/api/java/lang/RuntimeException.html}{RuntimeException}
  class)
   \end{itemize}
\end{frame}

Checked exceptions must be handled at some point in the program or it
will not even compile.  Though this may seem robust at first, simply
creating a checked exception does not ensure that you are handling it
properly, even if the handling is enforced by the compiler.  

The handling of runtime or unchecked exceptions is not enforced by the
compiler; this is the only difference between checked and unchecked
exceptions.  Java is somewhat unique in it's reliance on checked
exceptions. 

\subsection{try and catch}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{try and catch}}
\begin{lstlisting}
try {
  someMethod();
} catch(NullPointerException e) {
   e.printStackTrace();
   //your handling code.
}
\end{lstlisting}
\end{frame}


\subsection{try and catch  (with more than one exception)}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{try and catch  (with more than one exception)}}
\begin{lstlisting}
try {
  someMethod();
} catch(NullPointerException e) {
  //Only NullPointerExceptions handled here, 
  //others fall through to next handler.
} catch(IOException e1) {
  //Only IOExceptions are handled here, 
  //others fall through to next handler.
} catch(Exception e2 {
  //All exceptions are of type Exception 
  //and will thus be handled here
}
\end{lstlisting}

\end{frame}


\subsection{finally}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{finally}}
  If the exception handling code itself throws an exception, 
  the code in the \verb'finally' block takes care of it.

  \begin{lstlisting}
  try {
    someMethod();
  } catch(NullPointerException e) {
     e.printStackTrace();
     //your handling code.
  } finally {
    //always executed when the try 
    //block is invoked
  }
  \end{lstlisting}

\end{frame}
As long as we enter the \verb'try' block, the code in the 
\verb'finally' block is executed.

\subsection{throws}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{throws}}
  The \verb'throws' statement can come at the end of a method
  signature to indicate that the method may throw but will not handle
  a particular exception.

  \begin{lstlisting}
  public void readFile(String filename) 
           throws IOException { 
  ... 
  }
  public void start() {
    try {
       readFile("input.txt")
    } catch(IOException io) {
       return;
    }
    ...
  }
  \end{lstlisting}
\end{frame}
We use the \verb'readFile' method in \verb'start()'.  If we do not surround 
the method call with the try and catch statements (and if
we do not catch the IOException), the code will note compile


\subsection{throw}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{throw}}

  The \verb'throw' statement allows us as programmers to throw an
  exception if an error has occurred.  We can throw any exception
  already defined in the api, or we can create our own exceptions.

  \begin{lstlisting}
  public Object getElement() throws 
           NotYetInitialiazedException {
    if(element == null)
      throw new NotYetInitializedException();
    return element;
  }
  ...
  public class NotYetInitializedException 
           extends Exception {
   ...
  }
  \end{lstlisting}
\end{frame}

\section{Static Variables and Methods}

\subsection{Static Variables}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{Static Variables}}
  \begin{itemize}
  \item Static variables are associated with classes and not instances of class (objects).  
  \item Oftentimes constants are declared static.  They will not
  change and this will save memory (albeit a fairly small amount).
  \end{itemize}

\end{frame}


\subsection{Static Methods}
\begin{frame}[fragile]
  \mode<presentation>{\frametitle{Static Methods}}
  Static methods are similar to static variables in that they are
  associated with a class and not an object.  We can invoke a static
  method without creating an instance of an object.

  One place we use static method and variables often is:
  \begin{lstlisting}
    //out is a static variable of the class System
    //println is a static method of out
    System.out.println("Hello world!");
  \end{lstlisting}
\end{frame}

\end{document}
